using Gameplay.PVE.Entity;
using UnityEngine;

namespace Gameplay.PVE
{
    public class SelfMeleeMoveBehaviour : SelfMoveBehaviour
    {
        public override void Init(UnitBase unit, UnitRoadPath roadPath)
        {
            base.Init(unit, roadPath);
            if (roadPath is AStarRoadPath path)
            {
                path.SetForceEndCondition(() => { return true; });
            }
        }

        private float lostTargetTime;
        private bool lastLostLockTarget;
        
        protected override void UpdateGame()
        {
            base.UpdateGame();
            if (unit.Data.IsInSkill() || unit.Data.isInAction || unit.Data.isHiding || unit.Data.IsInAttack())
            {
                unit.Data.moveState = -1;
            }
            else
            {
                var lostTarget = unit.Data.lockTarget == null || unit.Data.lockTarget.Data.isDead;
                if (lostTarget && !lastLostLockTarget)
                {
                    //开始丢失目标
                    lostTargetTime = TimeManager.logicTime;
                }

                if (!lostTarget)
                {
                    lostTargetTime = 0;
                }
                if (lostTarget && lostTargetTime != 0 && TimeManager.logicTime - lostTargetTime > 0.2f)
                {
                    if (unit.Data.position.x > PveGlobalVlues.SelfMeleeLine2)
                    {
                        unit.Data.SetTargetPosition(unit.Data.birthPosition);
                    }
                    else
                    {
                        unit.Data.SetTargetForward(Vector3.right);
                    }
                }
                /*else
                {
                    unit.Data.SetTargetPosition(unit.Data.lockTarget.Data.position);
                }*/
                unit.Data.moveState = 2;
                lastLostLockTarget = lostTarget;
            }
           
        }
    }
}